package NC52_有效括号序列;

import java.util.Stack;

public class Solution {
    /**
     *
     * @param s string字符串
     * @return bool布尔型
     */
    public boolean isValid (String s) {
        // write code here
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < s.length(); i++){
            if (s.charAt(i) == '(' || s.charAt(i) == '[' || s.charAt(i) == '{'){
                stack.push(s.charAt(i));
            }else {
                if (stack.isEmpty()){
                    return false;
                }else {
                    if (s.charAt(i) == ')' && stack.peek() == '('){
                        stack.pop();
                    }

                    if (s.charAt(i) == ']' && stack.peek() == '['){
                        stack.pop();
                    }

                    if (s.charAt(i) == '}' && stack.peek() == '{'){
                        stack.pop();
                    }
                }
            }
        }
        if (stack.isEmpty()){
            return true;
        }else {
            return false;
        }
    }
}